﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;

namespace Persistencia
{
    class BrokerDetalle: Broker
    {
        #region constructor

        public BrokerDetalle()
        {
            this.conn = ConexionBD.getInstance();
        }

        #endregion

        #region methods

        public override void add(Object o)
        {
            Detalle detalleAux = (Detalle)o;
            this.conn.connect();
            string sql = "insert into Detalles(IdArticulo, Cantidad, SubTotal) values(" 
                + detalleAux.Articulo.OID + ", " + detalleAux.Cantidad + ", '" + detalleAux.Subtotal + "')";
            this.conn.execute(sql);            
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            Detalle detalleAux = (Detalle)o;
            string sql = "update Detalles set IdArticulo = " + detalleAux.Articulo.OID
                + ", Cantidad = " + detalleAux.Cantidad
                + ", SubTotal = '" + detalleAux.Subtotal
                + "' where IdDetalle = " + detalleAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            Detalle detalleAux = (Detalle)o;
            string sql = "delete * from Detalles where IdDetalle = " + detalleAux.OID; 
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            Detalle detalleAux = (Detalle)o;
            this.conn.connect();
            string sql = "Select * from Detalles where IdDetalle = " + detalleAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {                
                detalleAux.Articulo.OID = int.Parse(this.conn.getDataString("IdArticulo").ToString());
                detalleAux.Cantidad = int.Parse(this.conn.getDataString("Cantidad").ToString());
                detalleAux.Subtotal = double.Parse(this.conn.getDataString("SubTotal").ToString());
            }
            this.conn.disconnect();
        }

        public override List<Object> bringList(Object o)
        {
            string sql = "Select * from detalles";
            List<Object> list = new List<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                Detalle detalleAux = new Detalle();
                detalleAux.Articulo.OID = int.Parse(this.conn.getDataString("IdArticulo").ToString());
                detalleAux.Cantidad = int.Parse(this.conn.getDataString("Cantidad").ToString());
                detalleAux.Subtotal = double.Parse(this.conn.getDataString("SubTotal").ToString());
                detalleAux.OID = int.Parse(this.conn.getDataString("IdDetalle").ToString());
                list.Add(detalleAux);
            }
            this.conn.disconnect();
            return list;
        }

        public override int bringLastId()
        {
            int id = 0;
            this.conn.connect();
            string sql = "Select max(idDetalle) as idDetalle from Detalles";
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                id = int.Parse(this.conn.getDataString("idDetalle").ToString());
            }
            this.conn.disconnect();
            return id;
        }

        #endregion
    }
}
